<?php
namespace Client\Controller;
use Org\Net\Http;

use Think\Controller;
class ZYBController extends Controller {
	public function way(){
//		if(IS_POST){
			$actions = array('buy','refund','getCode');
			$action = $_GET['action'];
			if(in_array_case($action, $actions)){
				$http = new Http();
				$url = "http://boss.zhiyoubao.com/boss/service/code.htm";
				$xmlMsg = $this->$action($_GET);
				$post['xmlMsg'] = $xmlMsg;
				$post['sign'] = strtolower(md5("xmlMsg=".$xmlMsg."sdzfxqms"));
				$data['post'] = $post;
				$xmlstring =  $http->fsockopenDownload($url,$data);
				$json =  json_decode(json_encode((array) simplexml_load_string($xmlstring)), true);
				
				if(isset($json['img'])){
					header("Content-Type:image/png");
					echo base64_decode($json['img']);
				}else{
					header("Content-type: text/html; charset=utf-8");
					var_dump($json);
				}
			}
//		}	
	}
	public function refund($data){
		$xmlMsg = "<PWBRequest>
	  <transactionName>RETURN_TICKET_NUM_REQ</transactionName>
	  <header>
	    <application>SendCode</application>
	    <requestTime>".date('Y-m-d')."</requestTime>
	  </header>
	<identityInfo>
	    <corpCode>sdzfxqms</corpCode>
	    <userName>qms</userName>
	  </identityInfo>
	  <orderRequest>
	    <returnTicket>
	      <orderCode>{$data['orderCode']}</orderCode>
	      <orderType>scenic</orderType>
	      <returnNum>{$data['num']}</returnNum>
	    </returnTicket>
	  </orderRequest>
	</PWBRequest>";
		return $xmlMsg;
	}
	public function buy($data){
		$orderCode = "QMS".date('YmdHis');
		$orderPrice = $data['price']*$data['num'];
		$xmlMsg = "<PWBRequest>
  <transactionName>SEND_CODE_REQ</transactionName>
  <header>
    <application>SendCode</application>
    <requestTime>".date('Y-m-d')."</requestTime>
  </header>
  <identityInfo>
    <corpCode>sdzfxqms</corpCode>
    <userName>qms</userName>
  </identityInfo>
  <orderRequest>
    <order>
      <certificateNo>{$data['certificateNo']}</certificateNo>
      <linkName>{$data['linkName']}</linkName>
      <linkMobile>{$data['linkMobile']}</linkMobile>
      <orderCode>{$orderCode}</orderCode>
      <orderPrice>{$orderPrice}</orderPrice>
      <groupNo></groupNo>
      <payMethod>on_line</payMethod>
      <scenicOrders>
        <scenicOrder>
          <orderCode>{$orderCode}</orderCode>
          <price>{$data['price']}</price>
          <quantity>{$data['num']}</quantity>
          <totalPrice>{$orderPrice}</totalPrice>
          <occDate>".date('Y-m-d')."</occDate>
          <goodsCode>{$data['goodsCode']}</goodsCode>
          <goodsName>{$data['goodsName']}</goodsName>
        </scenicOrder>
      </scenicOrders>
    </order>
  </orderRequest>
</PWBRequest>";
		return $xmlMsg;
	}
	public function getCode($data){
		$xmlMsg = "<PWBRequest>
  <transactionName>SEND_CODE_IMG_REQ</transactionName>
  <header>
    <application>SendCode</application>
    <requestTime>".date('Y-m-d')."</requestTime>
  </header>
  <identityInfo>
    <corpCode>sdzfxqms</corpCode>
    <userName>qms</userName>
  </identityInfo>
  <orderRequest>
    <order>
      <orderCode>{$data['orderCode']}</orderCode>
    </order>
  </orderRequest>
</PWBRequest>";
		return $xmlMsg;
	}
}